/***********************************************************************
 * Module:  AddCommand.java
 * Author:  Nikola
 * Purpose: Defines the Class AddCommand
 ***********************************************************************/

package genedit.controller;

import genedit.model.graph.GraphElement;
import genedit.utility.PainterMap;

import java.util.Iterator;
import java.util.Set;

public class AddCommand extends Command {
	private Set<GraphElement> addedElements;

	/** @param isUndoable */
	public AddCommand(boolean isUndoable) {
		super(isUndoable);
	}

	public void execute() {
		Iterator<GraphElement> iterator = getIteratorAddedElements();
		while (iterator.hasNext()) {
			GraphElement current = iterator.next();
			getDocument().getModel().addElements(current);
			getDocument().addElementPainters(PainterMap.getInstance().getPainter(current));
		}
		getDocument().getModel().removeAllSelectedElements();
		getDocument().getWorkArea().repaint();
	}

	public void undo() {
		Iterator<GraphElement> iterator = getIteratorAddedElements();
		while (iterator.hasNext()) {
			GraphElement current = iterator.next();
			getDocument().getModel().removeElements(current);
		}
		getDocument().getModel().removeAllSelectedElements();
		getDocument().getWorkArea().repaint();
	}

	/** @pdGenerated default getter */
	public Set<GraphElement> getAddedElements() {
		if (addedElements == null)
			addedElements = new java.util.HashSet<GraphElement>();
		return addedElements;
	}

	/** @pdGenerated default iterator getter */
	public java.util.Iterator<GraphElement> getIteratorAddedElements() {
		if (addedElements == null)
			addedElements = new java.util.HashSet<GraphElement>();
		return addedElements.iterator();
	}

	/**
	 * @pdGenerated default setter
	 * @param newAddedElements
	 */
	public void setAddedElements(java.util.Collection<GraphElement> newAddedElements) {
		removeAllAddedElements();
		for (java.util.Iterator<GraphElement> iter = newAddedElements.iterator(); iter.hasNext();)
			addAddedElements((GraphElement) iter.next());
	}

	/**
	 * @pdGenerated default add
	 * @param newElementPainter
	 */
	public void addAddedElements(GraphElement newElementPainter) {
		if (newElementPainter == null)
			return;
		if (this.addedElements == null)
			this.addedElements = new java.util.HashSet<GraphElement>();
		if (!this.addedElements.contains(newElementPainter))
			this.addedElements.add(newElementPainter);
	}

	/**
	 * @pdGenerated default remove
	 * @param oldElementPainter
	 */
	public void removeAddedElements(GraphElement oldElementPainter) {
		if (oldElementPainter == null)
			return;
		if (this.addedElements != null)
			if (this.addedElements.contains(oldElementPainter))
				this.addedElements.remove(oldElementPainter);
	}

	/** @pdGenerated default removeAll */
	public void removeAllAddedElements() {
		if (addedElements != null)
			addedElements.clear();
	}

}